package com.medicine.web.controller.system;

import com.medicine.common.core.domain.entity.SysUser;
import com.medicine.common.utils.SecurityUtils;
import com.medicine.manage.block.domain.UserExtend;
import com.medicine.manage.service.block.IBlockService;
import com.medicine.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.core.domain.model.RegisterBody;
import com.medicine.common.utils.StringUtils;
import com.medicine.framework.web.service.SysRegisterService;
import com.medicine.system.service.ISysConfigService;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IBlockService blockService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody SysUser user)
    {

        Long[] roleIds = new Long[]{2L};
        user.setRoleIds(roleIds);
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }

        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));

        int result = userService.insertUser(user);

        UserExtend userExtend = blockService.createNormalSignUser(user.getUserId(), user.getUserName());
        userExtend.setUserId(user.getUserId());


        return toAjax(result);
    }
}
